package cn.tedu.mintmall.service.Impl;


import cn.tedu.mintmall.dto.AdminDTO;
import cn.tedu.mintmall.dto.AdminLoginDTO;
import cn.tedu.mintmall.entity.Admin;
import cn.tedu.mintmall.exception.MintmallServiceException;
import cn.tedu.mintmall.mapper.AdminMapper;
import cn.tedu.mintmall.restful.ResponseCode;
import cn.tedu.mintmall.service.IAdminService;
import cn.tedu.mintmall.vo.AdminDetailsVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class AdminImpl implements IAdminService {
    @Autowired
    AdminMapper adminMapper;

    @Value("${dirPath}")
    private String dirPath;

    @Override
    public int addAdmin(AdminDTO adminDTO, MultipartFile picFile) throws IOException {
       int testAdmin = adminMapper.getByUsername(adminDTO.getUsername());
        if (testAdmin!=0){
            throw new RuntimeException();
        }else {
           String filePath= headerJpg(picFile);
            Admin admin = new Admin();
            LocalDateTime now = LocalDateTime.now();
            BeanUtils.copyProperties(adminDTO,admin);
            admin.setAdminProfilePictureSrc(filePath);
            admin.setGmtCreate(now);
            admin.setGmtModified(now);
            return adminMapper.InsertAdmin(admin);
        }
    }

    @Override
    public int login(AdminLoginDTO adminLoginDTO, HttpSession session) {
        AdminDetailsVO adminDetails = adminMapper.getAdminDetailsVOByUsername(adminLoginDTO.getUsername());
        if (adminDetails==null){
            throw new RuntimeException("此管理员不存在");
        }else{
            if (adminDetails.getPassword().equals(adminLoginDTO.getPassword())){
                session.setAttribute("admin",adminDetails);
                return 1;
            }else{
               throw  new RuntimeException("密码错误");
            }
        }
    }

    @Override
    public void updateAdmin(String password,HttpSession session) {
        AdminDTO admin = (AdminDTO) session.getAttribute("admin");
        if (password.equals(admin.getPassword())){
            admin.setPassword(password);
            adminMapper.updateAdmin(admin);
        }else{
            throw new MintmallServiceException(ResponseCode.BAD_REQUEST,"原始密码错误,请重新输入");
        }
    }

    @Override
    public void updateHeaderJpg(HttpSession session,MultipartFile picFile) throws IOException {
        String path = headerJpg(picFile);
        Admin admin = (Admin) session.getAttribute("admin");
        String username = admin.getUsername();
        adminMapper.updateHeaderjpg(username,path);
    }

    private String headerJpg (MultipartFile picFile) throws IOException {
        System.out.println("picFile = " + picFile);
        //得到文件的原始文件名  a.jpg
        String filename = picFile.getOriginalFilename();;
        //得到后缀名
        String suffix = filename.substring(filename.lastIndexOf("."));
        //得到唯一文件名
        filename = UUID.randomUUID()+suffix;
        System.out.println("文件名:"+filename);
        //准备保存图片的文件夹路径
//        String dirPath = "E:/files";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();//创建文件夹
        }
        //得到文件的完成路径
        String filePath = dirPath+"/"+filename;
        //把文件保存到此路径
        picFile.transferTo(new File(filePath));
        return filePath;
    }
}
